package cn.keepme.common.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

import org.apache.commons.lang.time.DateFormatUtils;

public class Utils {
	
	public static String getUUID(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	/**
	 * 格式化日期
	 * pattern如果为null则默认格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateFormat(Date date,String pattern){
		if(pattern==null){
			pattern="yyyy-MM-dd HH:mm:ss";
		}
		return DateFormatUtils.format(date, pattern);
	}
	
	/**
	 * 格式化日期
	 * pattern如果为null则默认格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateFormat(Timestamp date,String pattern){
		if(pattern==null){
			pattern="yyyy-MM-dd HH:mm:ss";
		}
		return DateFormatUtils.format(date, pattern);
	}
	
	/**
	 * 将String类型的日期格式化为Date类型
	 * pattern如果为null则默认格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDate(String date,String pattern) throws ParseException{
		if(pattern==null){
			pattern="yyyy-MM-dd HH:mm:ss";
		}
		DateFormat formater = new SimpleDateFormat(pattern);
		return formater.parse(date);
	}
	
	/**
	 * 格式化数字
	 * @param num
	 * @param value 保留的小数位
	 * @return
	 */
	public static String numberFormat(double num,int value){
		NumberFormat formater=DecimalFormat.getInstance();
		formater.setMaximumFractionDigits(value);
		formater.setGroupingUsed(false);
		return formater.format(num);
	}
	
	/**
	 * 格式化数字
	 * @param str
	 * @param value 保留的小数位
	 * @return
	 */
	public static String numberFormat(String str,int value){
		double num=Double.parseDouble(str);
		NumberFormat formater=DecimalFormat.getInstance();
		formater.setMaximumFractionDigits(value);
		formater.setGroupingUsed(false);
		return formater.format(num);
	}
	
	/**
	 * util.Date转换为sql.Date
	 * @param date
	 * @return
	 */
	public static java.sql.Date utilToSql(Date date) {
		return new java.sql.Date(date.getTime());
	}
	
	/**
	 * 返回传入的日期的前N天或后N天的日期
	 * 
	 * @param appDate
	 * @param format
	 * @param days
	 * @return
	 */
	public static String getFutureDay(String appDate, String format, int days) {
		String future = "";
		try {
			Calendar calendar = GregorianCalendar.getInstance();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			Date date = simpleDateFormat.parse(appDate);
			calendar.setTime(date);
			calendar.add(Calendar.DATE, days);
			date = calendar.getTime();
			future = simpleDateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return future;
	}
	
	/**
	 * 取得某年某月的第一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month-1);
		cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
		String dateStr=dateFormat(cal.getTime(),"yyyy-MM-dd");
		return dateStr;
	}
	
	/**
	 * 取得某年某月的最后一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month-1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, value);
		String dateStr=dateFormat(cal.getTime(),"yyyy-MM-dd");
		return dateStr;
	}
	
	
	
	/**
	 * 获取当月第一天的日期
	 * @return
	 */
	public static String getMonthFirstDay(){
		Calendar cal = Calendar.getInstance();
		Calendar f = (Calendar)cal.clone();
		f.clear();
		f.set(Calendar.YEAR,cal.get(Calendar.YEAR));
		f.set(Calendar.MONTH,cal.get(Calendar.MONTH));
		String dateStr=dateFormat(f.getTime(),"yyyy-MM-dd");
		return dateStr;
	}


	/**
	 * 获取当月最后一天的日期
	 * @return
	 */
	public static String getMonthLastDay(){
		Date date=new Date();
		Calendar cal = Calendar.getInstance();   
		cal.setTime(date);   
		cal.set(Calendar.DAY_OF_MONTH, 1);   
		cal.add(Calendar.MONTH, 1);   
		cal.add(Calendar.DATE, -1);
		String dateStr=dateFormat(cal.getTime(),"yyyy-MM-dd");
		return dateStr;
	}
	
}
